package jp.co.tms.internal.util;

import java.util.UUID;

public class NumberUtil {
	
	public static byte[] intToByteArray(final int integer){
		byte[] byteArray = new byte[Integer.SIZE/Byte.SIZE];
		int byteNum = (40 - Integer.numberOfLeadingZeros (integer < 0 ? ~integer : integer))/Byte.SIZE;
		for (int n = 0; n < byteNum; n++)
			byteArray[Integer.SIZE/Byte.SIZE - (n+1) ] = (byte) (integer>>> (n * Byte.SIZE));
		return byteArray;
	}
	
	public static int byteArrayToInt(byte[] byteValue){   
        if (byteValue == null || byteValue.length == 0)
            return 0;
        
        int intValue = 0;
        if (byteValue.length == 1){
        	intValue = byteValue[0] & 0xFF;
        }
        else{
            intValue  = byteValue[0] & 0xFF;
            intValue |= (byteValue[1] << 8)  & 0xFF00;
            intValue |= (byteValue[2] << 16) & 0xFF0000;
            intValue |= (byteValue[3] << 24) & 0xFF000000;        	
        }

        return intValue;   
	} 
	
	public static byte[] UUIDtoBytes(UUID id){
		return id.toString().getBytes();
	}
 }
